//
//  LGKNoteSearchViewController.m
//  LGKNote
//
//  Created by 刘亚军 on 2023/5/31.
//

#import "LGKNoteSearchViewController.h"
#import "LGKConst.h"
#import <YJBaseModule/YJBTableView.h>
#import "LGKNoteMainCell.h"
#import "LGKNoteMainDataModel.h"
#import "LGKNoteMainCell.h"
#import "LGKNoteMainModel.h"
#import "LGKNoteEditViewController.h"
@interface LGKNoteSearchViewController ()<UITableViewDelegate,UITableViewDataSource,YJBTableViewRefreshDelegate>
@property (nonatomic,strong) YJBTableView *tableView;
@property (nonatomic,strong) UILabel *searchTipL;
@end

@implementation LGKNoteSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataModel.ownTableView = self.tableView;
    [self.view addSubview:self.searchTipL];
    [self.searchTipL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(10);
        make.left.equalTo(self.view).offset(20);
        make.height.mas_equalTo(25);
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.centerX.equalTo(self.view);
        make.top.equalTo(self.searchTipL.mas_bottom);
    }];
}
- (void)setSearchKey:(NSString *)searchKey{
    searchKey = [searchKey yj_deleteWhitespaceCharacter];
    _searchKey = searchKey;
    self.dataModel.searchKey = searchKey;
    [self yj_loadGifData];
}
- (void)yj_updateData{
    NSMutableAttributedString *attr = [NSString stringWithFormat:@"共搜索到%@份笔记",@([(LGKNoteMainDataModel *)self.dataModel totalCount])].yj_toMutableAttributedString;
    [attr yj_setFont:IsIPad ? 16 : 15];
    [attr yj_setColor:LG_ColorWithHex(0x999999)];
    [attr yj_setNumberForegroundColor:LG_ColorWithHex(0x0099ff) font:IsIPad ? 15 : 14];
    self.searchTipL.attributedText = attr;
    [self.tableView reloadData];
}
#pragma mark - YJBTableViewRefreshDelegate
- (void)tableViewHeaderDidRefresh{
    [self.dataModel yj_loadTableFirstPage];
}
- (void)tableViewFooterDidRefresh{
    [self.dataModel yj_loadTableNextPage];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataModel.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGKNoteMainCell *cell = nil;
    LGKNoteMainModel *model = self.dataModel.models[indexPath.row];
    if (model.mixTextImage){
        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(LGKNoteMainTextImgCell.class) forIndexPath:indexPath];
    }else{
        if (!IsArrEmpty(model.imgUrlArr)){
            cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(LGKNoteMainImgCell.class) forIndexPath:indexPath];
        }else{
            cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(LGKNoteMainTextCell.class) forIndexPath:indexPath];
        }
    }
    cell.searchKey = self.dataModel.searchKey;
    cell.mainModel = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LGKNoteMainModel *model = self.dataModel.models[indexPath.row];
    LGKNoteEditViewController *editVC =  [[LGKNoteEditViewController alloc] initWithDataModelName:@"LGKNoteEditDataModel"];
    editVC.marqueeTitle = @"编辑笔记";
    editVC.dataModel.resID = model.NoteID;
    __weak typeof(self) weakSelf = self;
    editVC.updateBlock = ^{
        if (!IsArrEmpty(weakSelf.dataModel.models)){
            [weakSelf tableViewHeaderDidRefresh];
        }else{
            [weakSelf yj_loadGifData];
        }
        if (weakSelf.updateBlock){
            weakSelf.updateBlock();
        }
    };
    [self.navigationController pushViewController:editVC animated:YES];
}
#pragma mark - lazy
- (UILabel *)searchTipL{
    if (!_searchTipL) {
        _searchTipL = [UILabel new];
    }
    return _searchTipL;
}
- (YJBTableView *)tableView{
    if (!_tableView){
        _tableView = [[YJBTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView installRefreshHeader:YES footer:YES];
        _tableView.backgroundColor = LG_ColorWithHex(0xf7f7f7);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100;
        _tableView.refreshDelegate = self;
        [_tableView registerClass:[LGKNoteMainTextImgCell class] forCellReuseIdentifier:NSStringFromClass([LGKNoteMainTextImgCell class])];
        [_tableView registerClass:[LGKNoteMainImgCell class] forCellReuseIdentifier:NSStringFromClass([LGKNoteMainImgCell class])];
        [_tableView registerClass:[LGKNoteMainTextCell class] forCellReuseIdentifier:NSStringFromClass([LGKNoteMainTextCell class])];
    }
    return _tableView;
}
@end
